home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / importers and exporters / carbon qt graphic import / source / preferences.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  13.4 KB  |  443 lines

  1. /*
  2.     File:        Preferences.c
  3.     
  4.     Description:Code that displays the preferences dialog and reads and writes the preferences
  5.                 file.
  6.  
  7.     Author:        MC
  8.  
  9.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  10.     
  11.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  12.                 ("Apple") in consideration of your agreement to the following terms, and your
  13.                 use, installation, modification or redistribution of this Apple software
  14.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  15.                 please do not use, install, modify or redistribute this Apple software.
  16.  
  17.                 In consideration of your agreement to abide by the following terms, and subject
  18.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  19.                 copyrights in this original Apple software (the "Apple Software"), to use,
  20.                 reproduce, modify and redistribute the Apple Software, with or without
  21.                 modifications, in source and/or binary forms; provided that if you redistribute
  22.                 the Apple Software in its entirety and without modifications, you must retain
  23.                 this notice and the following text and disclaimers in all such redistributions of
  24.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  25.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  26.                 Apple Software without specific prior written permission from Apple.  Except as
  27.                 expressly stated in this notice, no other rights or licenses, express or implied,
  28.                 are granted by Apple herein, including but not limited to any patent rights that
  29.                 may be infringed by your derivative works or by other works in which the Apple
  30.                 Software may be incorporated.
  31.  
  32.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  33.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  34.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  36.                 COMBINATION WITH YOUR PRODUCTS.
  37.  
  38.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  39.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  40.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  42.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  43.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  44.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.                 
  46.     Change History (most recent first):
  47.  
  48. */
  49.  
  50. #define TARGET_API_MAC_CARBON 1
  51.  
  52. #include <Resources.h>
  53. #include <Folders.h>
  54. #include <TextUtils.h>
  55. #include <Errors.h>
  56. #include <LowMem.h>
  57. #include <Controls.h>
  58. #include <fp.h>
  59.  
  60. #include "Preferences.h"
  61. #include "WindowCode.h"
  62. #include "Structs.h"
  63. #include "Defines.h"
  64.  
  65. /*
  66.     Preferences file is a 'pref' resource
  67.     first long (byte 0-3) is delay during slide show in tics
  68.     next byte (byte 4) is flag for if to display files in reverse order
  69.       if 0 then first file in 'odoc' event is opened first (so it's at the bottom of the window list)
  70.       if 1 then last file in 'odoc' event is opened first (so it's at the bottom of the window list)
  71. */
  72.  
  73. extern    PreferencesHandle            gPreferences;
  74. extern    Boolean                        gPrefsChanged;
  75.  
  76. static OSErr    FindPrefsFile (FSSpec * theSpec) {
  77.     OSErr                err;
  78.     long                foundDirID;
  79.     short                foundVRefNum;
  80.     Str255                prefsFileName;
  81.  
  82.     err = FindFolder (kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &foundVRefNum, &foundDirID);
  83.  
  84.     if (err == noErr) {
  85.         GetIndString (prefsFileName, kPrefsFileNameResID, kPrefsFileNameIndex);
  86.         err = ResError ();
  87.         if (prefsFileName == nil) {
  88.             err = resNotFound;
  89.         }
  90.     }
  91.  
  92.     if (err == noErr) {
  93.         err = FSMakeFSSpec (foundVRefNum, foundDirID, prefsFileName, theSpec);
  94.     }
  95.  
  96.     return err;
  97. }
  98.  
  99. OSErr    CreatePrefs (void) {
  100.     FSSpec                theSpec;
  101.     OSErr                err;
  102.     FInfo                finderInfo;
  103.  
  104.     err = FindPrefsFile (&theSpec);
  105.  
  106.     if (err == fnfErr) {
  107.         FSpCreateResFile (&theSpec, kCreatorType, kPrefResType, smSystemScript);
  108.         err = ResError ();
  109.     } else {
  110.         FSpGetFInfo (&theSpec, &finderInfo);
  111.         if (finderInfo.fdCreator == kCreatorType) {
  112.             err = FSpDelete (&theSpec);
  113.             if (err == noErr) {
  114.                 FSpCreateResFile (&theSpec, kCreatorType, kPrefResType, smSystemScript);
  115.                 err = ResError ();
  116.             }
  117.         }
  118.     }
  119.  
  120.     return err;
  121. }
  122.  
  123. OSErr    InitPrefs (void) {
  124.     OSErr                err;
  125.  
  126.     gPreferences = (PreferencesHandle)NewHandle (sizeof (Preferences));
  127.     err = MemError ();
  128.  
  129.     if (err == noErr) {
  130.         (**gPreferences).slideShowPause = 120;    // two seconds in ticks
  131.         (**gPreferences).quality = codecNormalQuality;
  132.         (**gPreferences).openInReverseOrder = false;
  133.         (**gPreferences).scanSubFolders = false;
  134.         (**gPreferences).randomSlideShow = false;
  135.     }
  136.  
  137.     return err;
  138. }
  139.  
  140. OSErr    ReadPrefs (void) {
  141.     FSSpec                theSpec;
  142.     OSErr                err;
  143.     short                resRef;
  144.  
  145.     err = FindPrefsFile (&theSpec);
  146.  
  147.     if (err == noErr) {
  148.         resRef = FSpOpenResFile (&theSpec, fsRdPerm);
  149.         if (resRef == -1) {
  150.             err = ResError ();
  151.         }
  152.     } else {
  153.         resRef = -1;
  154.     }
  155.  
  156.     if (err == noErr) {
  157.         gPreferences = (PreferencesHandle)Get1Resource (kPrefResType, kPrefResID);
  158.         err = ResError ();
  159.         if (gPreferences == nil) {
  160.             err = resNotFound;
  161.         }
  162.     }
  163.  
  164.     if (err == noErr) {
  165.         DetachResource ((Handle)gPreferences);
  166.     }
  167.  
  168.     if (resRef != -1) {
  169.         CloseResFile (resRef);
  170.     }
  171.  
  172.     return err;
  173. }
  174.  
  175. OSErr    WritePrefs (void) {
  176.     Handle                temp;
  177.     FSSpec                theSpec;
  178.     OSErr                err;
  179.     short                resRef;
  180.  
  181.     err = FindPrefsFile (&theSpec);
  182.  
  183.     if (err == noErr) {
  184.         resRef = FSpOpenResFile (&theSpec, fsRdWrPerm);
  185.         if (resRef == -1) {
  186.             err = ResError ();
  187.         }
  188.     }
  189.  
  190.     if (err == noErr) {
  191.         temp = Get1Resource (kPrefResType, kPrefResID);
  192.         err = ResError ();
  193.         if (temp == nil) {
  194.             err = resNotFound;
  195.         }
  196.     }
  197.  
  198.     if (err == noErr) {
  199.         RemoveResource (temp);
  200.     } else if (err == resNotFound) {
  201.         err = noErr;
  202.     }
  203.  
  204.     if (err == noErr) {
  205.         AddResource ((Handle)gPreferences, kPrefResType, kPrefResID, nil);
  206.     }
  207.  
  208.     if (resRef != -1) {
  209.         CloseResFile (resRef);
  210.     }
  211.  
  212.     DisposeHandle (temp);
  213.  
  214.     return err;
  215. }
  216.  
  217. static pascal Boolean ModalDialogFilter (DialogPtr theDialog, EventRecord *theEvent, short *itemHit) {
  218.     Boolean                        result;
  219.     OSErr                        err;
  220.     ModalFilterUPP                standardProc;
  221.  
  222.     result = false;
  223.     if ((theEvent->what == updateEvt) && (DialogPtr)theEvent->message != theDialog) {
  224.         err = DispatchWindowUpdate ((WindowPtr)theEvent->message);
  225.     } else if ((theEvent->what == activateEvt) && (DialogPtr)theEvent->message != theDialog) {
  226.         DoActivate (theEvent, true);
  227.     } else {
  228.         err = GetStdFilterProc (&standardProc);
  229.         if (err == noErr) {
  230.             result = (Boolean)CallModalFilterProc (standardProc, theDialog, theEvent, itemHit);
  231.         }
  232.     }
  233.  
  234.     return result;
  235. }
  236.  
  237. static void SetRadioGroup (DialogPtr theDialog, short firstRadio, short lastRadio, short onRadio) {
  238.     short                        i;
  239.     DialogItemType                        itemType;
  240.     ControlHandle                item;
  241.     Rect                        box;
  242.  
  243.     for (i = firstRadio; i <= lastRadio; i++) {
  244.         if (i != onRadio) {
  245.             GetDialogItem (theDialog, i, &itemType, (Handle*)&item, &box);
  246.             SetControlValue (item, 0);
  247.         } else {
  248.             GetDialogItem (theDialog, i, &itemType, (Handle*)&item, &box);
  249.             SetControlValue (item, 1);
  250.         }
  251.     }
  252. }
  253. OSErr    DoPreferencesDialog (void) {
  254.     OSErr                        err;
  255.     DialogPtr                    prefsDialog;
  256.     short                        itemHit;
  257.     DialogItemType                        itemType;
  258.     ControlHandle                item;
  259.     Rect                        box;
  260.     Str255                        pauseAsString;
  261.     Boolean                        done;
  262.     double                        pauseTime;
  263.     CodecQ                        origQuality;
  264.     WindowInfoHandle            winInfo;
  265.     ModalFilterUPP                filterProcUPP;
  266.     Handle                        itlHandle;
  267.     unsigned char*                inString;
  268.     NumFormatString                outString;
  269.     NumberParts                    numParts;
  270.     FormatResultType            formatStatus;
  271.     long                        offset,
  272.                                 length;
  273.     extended80                    x80;
  274.     Rect                        winRect;
  275.  
  276.     done = false;
  277.     inString = "\p###,###.#######";
  278.     err = noErr;
  279.  
  280.     prefsDialog = GetNewDialog (kPrefsDialogResID, nil, (WindowPtr)-1L);
  281.  
  282.     if (prefsDialog == nil) {
  283.         err = resNotFound;
  284.     }
  285.  
  286.     if (err == noErr) {
  287.         err = SetDialogDefaultItem (prefsDialog, dOKButton);
  288.     }
  289.  
  290.     if (err == noErr) {
  291.         err = SetDialogCancelItem (prefsDialog, dCancelButton);
  292.     }
  293.  
  294.     if (err == noErr) {
  295.         err = SetDialogTracksCursor (prefsDialog, true);
  296.     }
  297.  
  298.     if (err == noErr) {
  299.         GetDialogItem (prefsDialog, dReverseOrderPref, &itemType, (Handle*)&item, &box);
  300.         SetControlValue (item, (**gPreferences).openInReverseOrder);
  301.  
  302.         GetDialogItem (prefsDialog, dScanSubFolders, &itemType, (Handle*)&item, &box);
  303.         SetControlValue (item, (**gPreferences).scanSubFolders);
  304.  
  305.         GetDialogItem (prefsDialog, dRandomSlideShow, &itemType, (Handle*)&item, &box);
  306.         SetControlValue (item, (**gPreferences).randomSlideShow);
  307.  
  308.         origQuality = (**gPreferences).quality;
  309.         switch (origQuality) {
  310.             case codecMinQuality:
  311.                 GetDialogItem (prefsDialog, dMinimumQuality, &itemType, (Handle*)&item, &box);
  312.                 SetControlValue (item, 1);
  313.                 break;
  314.             case codecLowQuality:
  315.                 GetDialogItem (prefsDialog, dLowQuality, &itemType, (Handle*)&item, &box);
  316.                 SetControlValue (item, 1);
  317.                 break;
  318.             case codecNormalQuality:
  319.                 GetDialogItem (prefsDialog, dNormalQuality, &itemType, (Handle*)&item, &box);
  320.                 SetControlValue (item, 1);
  321.                 break;
  322.             case codecHighQuality:
  323.                 GetDialogItem (prefsDialog, dHighQuality, &itemType, (Handle*)&item, &box);
  324.                 SetControlValue (item, 1);
  325.                 break;
  326.             case codecMaxQuality:
  327.                 GetDialogItem (prefsDialog, dMaxQuality, &itemType, (Handle*)&item, &box);
  328.                 SetControlValue (item, 1);
  329.                 break;
  330.         }
  331.  
  332.         GetDialogItem (prefsDialog, dSlideShowTime, &itemType, (Handle*)&item, &box);
  333.         GetIntlResourceTable (smCurrentScript, smNumberPartsTable, &itlHandle, &offset, &length);
  334.         if (itlHandle != nil) {
  335.             pauseTime = (**gPreferences).slideShowPause / 60.0;
  336.             dtox80 (&pauseTime, &x80);
  337.             BlockMoveData (((char*)*itlHandle) + offset, &numParts, length);
  338.             formatStatus = (FormatResultType)StringToFormatRec (inString, &numParts, &outString);
  339.  
  340.             if (formatStatus == fFormatOK) {
  341.                 formatStatus = (FormatResultType)ExtendedToString (&x80, &outString, &numParts, pauseAsString);
  342.             }
  343.  
  344.             ReleaseResource (itlHandle);
  345.         }
  346.         SetDialogItemText ((Handle)item, pauseAsString);
  347.         SelectDialogItemText (prefsDialog, dSlideShowTime, 0, 32767);
  348.  
  349.         ShowWindow (GetDialogWindow(prefsDialog));
  350.         SetPort (GetWindowPort(GetDialogWindow(prefsDialog)));
  351.     }
  352.  
  353.     filterProcUPP = NewModalFilterProc (ModalDialogFilter);
  354.  
  355.     if (err == noErr) {
  356.         do {
  357.             ModalDialog (filterProcUPP, &itemHit);
  358.             switch (itemHit) {
  359.                 case dOKButton:
  360.                     GetDialogItem (prefsDialog, dSlideShowTime, &itemType, (Handle*)&item, &box);
  361.                     GetDialogItemText ((Handle)item, pauseAsString);
  362.                     GetIntlResourceTable (smCurrentScript, smNumberPartsTable, &itlHandle, &offset, &length);
  363.                     if (itlHandle != nil) {
  364.                         BlockMoveData (((char*)*itlHandle) + offset, &numParts, length);
  365.                         formatStatus = (FormatResultType)StringToFormatRec (inString, &numParts, &outString);
  366.  
  367.                         if (formatStatus == fFormatOK) {
  368.                             formatStatus = (FormatResultType)StringToExtended (pauseAsString, &outString, &numParts, &x80);
  369.                         }
  370.  
  371.                         ReleaseResource (itlHandle);
  372.                         pauseTime = x80tod (&x80);
  373.                     } else {
  374.                         pauseTime = 3.0;
  375.                     }
  376.                     (**gPreferences).slideShowPause = (unsigned long)(pauseTime * 60.0);
  377.  
  378.                     GetDialogItem (prefsDialog, dReverseOrderPref, &itemType, (Handle*)&item, &box);
  379.                     (**gPreferences).openInReverseOrder = (Boolean)GetControlValue (item);
  380.                     
  381.                     GetDialogItem (prefsDialog, dScanSubFolders, &itemType, (Handle*)&item, &box);
  382.                     (**gPreferences).scanSubFolders = (Boolean)GetControlValue (item);
  383.  
  384.                     GetDialogItem (prefsDialog, dRandomSlideShow, &itemType, (Handle*)&item, &box);
  385.                     (**gPreferences).randomSlideShow = (Boolean)GetControlValue (item);
  386.  
  387.                     GetDialogItem (prefsDialog, dMaxQuality, &itemType, (Handle*)&item, &box);
  388.                     if (GetControlValue (item))
  389.                         (**gPreferences).quality = codecMaxQuality;
  390.                     GetDialogItem (prefsDialog, dHighQuality, &itemType, (Handle*)&item, &box);
  391.                     if (GetControlValue (item))
  392.                         (**gPreferences).quality = codecHighQuality;
  393.                     GetDialogItem (prefsDialog, dNormalQuality, &itemType, (Handle*)&item, &box);
  394.                     if (GetControlValue (item))
  395.                         (**gPreferences).quality = codecNormalQuality;
  396.                     GetDialogItem (prefsDialog, dLowQuality, &itemType, (Handle*)&item, &box);
  397.                     if (GetControlValue (item))
  398.                         (**gPreferences).quality = codecLowQuality;
  399.                     GetDialogItem (prefsDialog, dMinimumQuality, &itemType, (Handle*)&item, &box);
  400.                     if (GetControlValue (item))
  401.                         (**gPreferences).quality = codecMinQuality;
  402.                     gPrefsChanged = true;
  403.                 case dCancelButton:
  404.                     done = true;
  405.                     break;
  406.                 case dMaxQuality:
  407.                 case dHighQuality:
  408.                 case dNormalQuality:
  409.                 case dLowQuality:
  410.                 case dMinimumQuality:
  411.                     SetRadioGroup (prefsDialog, dMaxQuality, dMinimumQuality, itemHit);
  412.                     break;
  413.                 default:
  414.                     GetDialogItem (prefsDialog, itemHit, &itemType, (Handle*)&item, &box);
  415.                     if (itemType == chkCtrl + ctrlItem) {
  416.                         SetControlValue (item, !GetControlValue (item));
  417.                     }
  418.             }
  419.         } while (done == false);
  420.  
  421.         if (origQuality != (**gPreferences).quality) {
  422.             // The user changed the quality settings so redraw all the windows.
  423.             WindowPtr        theWindow;
  424.  
  425.             theWindow = FrontWindow ();
  426.             while (theWindow != nil) {
  427.                 winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  428.                 if (winInfo != nil && (**winInfo).sig == kMySig && (**winInfo).winType == kGraphPicWinType) {
  429.                     GetWindowPortBounds (theWindow, &winRect);
  430.                     InvalWindowRect (theWindow, &winRect);
  431.                 }
  432.                 theWindow = GetNextWindow (theWindow);
  433.             }
  434.         }
  435.  
  436.         DisposeDialog (prefsDialog);
  437.     }
  438.  
  439.     DisposeModalFilterUPP (filterProcUPP);
  440.  
  441.     return err;
  442. }
  443.